#!/usr/local/bin/python2.7
# encoding: utf-8
'''
NervuresDesignTool -- Outil de conception/simulation de parapentes Nervures
Classe xxx
Description :
@module : test
@author:      puiseux
@copyright:   2013 Nervures. All rights reserved.      
@license:    LGPL
@contact:    be@nervures.com, pierre@puiseux.name
@deffield    creation: 14 mars 2013
'''
from config import SOURCES_DIR
from utilitaires import Path
from PyQt4.QtGui import (QApplication, QLabel, QTextEdit, QFrame, QGridLayout, QWidget, QSizePolicy, QSpacerItem)
from PyQt4.uic import compileUi, loadUi
import sys, StringIO

    
if __name__ == "__main__":
    '''Pour visualiser le widget toto.ui à coté du code généré par pyuic ou loadUi'''
    app = QApplication(sys.argv)
#    uipath = Path(SOURCES_DIR,"gui","ui","simple.ui")
    uipath = Path(SOURCES_DIR,"gui","ui","test1.ui")
    # Le widget principal
    Frame = QFrame()
    Frame.resize(800, 600)
    Frame.setFrameShape(QFrame.StyledPanel)
    Frame.setFrameShadow(QFrame.Raised)
    #La grille
    gridLayout = QGridLayout(Frame)
    #Le widget à regarder on le met dans une frame pour l'encadrer
    wframe = QFrame(Frame)
    wframe.setFrameShape(QFrame.Box)
    wframe.setFrameShadow(QFrame.Raised)
    wframe.setLineWidth(3)
    wframe.setToolTip(u'Ici le <b>widget</b> dont le source Python est à droite.')
    wgridLayout = QGridLayout(wframe)
    widget = loadUi(uipath)
    widget.setParent(wframe)
    wgridLayout.addWidget(widget, 0, 0, 1, 1)
    gridLayout.addWidget(wframe, 0,0,1,1)
#    sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
#    sizePolicy.setHorizontalStretch(0)
#    sizePolicy.setVerticalStretch(0)
#    sizePolicy.setHeightForWidth(widget.sizePolicy().hasHeightForWidth())
#    widget.setSizePolicy(sizePolicy)
#    text = QLabel(u'Le widget encadré ci dessus')
    spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
    gridLayout.addItem(spacerItem, 0, 1, 1, 1)
    #Le source python du widget
    iosource = StringIO.StringIO()
    compileUi(uipath, iosource)
    source = iosource.getvalue()
    print source
    # Le source dans un widget QTextEdit
    te = QTextEdit(Frame)
    te.setText(source)
    te.setToolTip(u'Ici le <b>source</b> du widget de gauche')
#    sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
#    sizePolicy.setHorizontalStretch(1)
#    sizePolicy.setVerticalStretch(1)
#    sizePolicy.setHeightForWidth(te.sizePolicy().hasHeightForWidth())
#    te.setSizePolicy(sizePolicy)
    gridLayout.addWidget(te, 0, 2, 2, 1)
    spacerItem1 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
    gridLayout.addItem(spacerItem1, 1, 0, 1, 1)
    gridLayout.setColumnStretch(2, 2)
#    widget.layout().addWidget(bsource)
    Frame.show()

    sys.exit(app.exec_())